package com.xiaoxi.exam.job;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xiaoxi.exam.bean.Grep;
import com.xiaoxi.exam.dao.ExamToeflMapper;
import com.xiaoxi.exam.model.ExamToefl;
import com.xiaoxi.exam.model.ExamToeflExample;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Objects;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class GrepToeflExamJob implements Runnable {

    private ExamToeflMapper examToeflMapper;

    public GrepToeflExamJob(ExamToeflMapper examToeflMapper) {
        this.examToeflMapper = examToeflMapper;
    }

    public static final ObjectMapper mapper = new ObjectMapper();

    private final static String[] cities = ("北京,上海,大连,石家庄,深圳,天津,杭州,苏州,海口,厦门,哈尔滨,成都,南京,延吉,武汉,"
            + "广州,潍坊,南通,长沙,济南,兰州,合肥,长春,西安,郑州,福州,重庆,威海,"
            + "扬州,洛阳,临沂,乌鲁木齐,青岛,呼和浩特,开封,沈阳,昆明,烟台,"
            + "绵阳,汕头,南昌,太原,宁波,太仓,南宁").split(",");

    @Override
    public void run() {
        while (true) {
            for (String city : cities) {
                Process process = null;
                try {
                    System.out.println("start login ...");
                    String[] arg = new String[]{"python", "C:\\Toefl.py", city};
                    process = Runtime.getRuntime().exec(arg);
                    BufferedReader in = new BufferedReader(
                            new InputStreamReader(process.getInputStream(), "GBK"));
                    String line;
                    while ((line = in.readLine()) != null) {
                        Grep grep = mapper.readValue(line, Grep.class);
                        if (Objects.nonNull(grep.getCity())) {
                            String examCity = grep.getCity().contains(" ") ? grep.getCity().split(" ")[1] :
                                    grep.getCity();
                            ExamToeflExample examToeflExample = new ExamToeflExample();
                            examToeflExample.or().andCityEqualTo(examCity).andExamCodeEqualTo(grep.getCode())
                                    .andExamMonthEqualTo(grep.getMonth()).andDeletedAtIsNull();
                            List<ExamToefl> examToefls = examToeflMapper.selectByExample(examToeflExample);
                            if (examToefls.isEmpty()) {
                                ExamToefl examToefl = new ExamToefl();
                                examToefl.setCity(examCity);
                                examToefl.setCost(grep.getFee().split("￥")[1]);
                                examToefl.setExamCode(grep.getCode());
                                examToefl.setExamName(grep.getName());
                                examToefl.setExamMonth(grep.getMonth());
                                examToefl.setExamTime(grep.getDate());
                                examToefl.setStatus(grep.getStatus());
                                examToeflMapper.insert(examToefl);
                            }

                        }
                    }
                    in.close();
                    process.waitFor();
                } catch (Exception e) {
                    e.printStackTrace();
                    continue;
                }
            }
        }
    }
}
